package com.hfzy.ihk.web.websocket.conf;

import com.hfzy.ihk.web.websocket.support.swx.SwxWebSocketHandshakeInterceptor;
import com.hfzy.ihk.web.websocket.support.swx.handler.SwxWebSocketHandler;
import com.hfzy.ihk.web.websocket.support.toApp.AppWebSocketHandshakeInterceptor;
import com.hfzy.ihk.web.websocket.support.toApp.handler.AppWebSocketHandler;
import com.hfzy.ihk.web.websocket.support.toBrowser.BrowserWebSocketHandshakeInterceptor;
import com.hfzy.ihk.web.websocket.support.toBrowser.PluginWebSocketHandshakeInterceptor;
import com.hfzy.ihk.web.websocket.support.toBrowser.handler.BrowserWebSocketHandler;
import com.hfzy.ihk.web.websocket.support.toBrowser.handler.ChromePluginWebSocketHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;

/**
 * Created by Administrator on 2018/7/31.
 *
 * @author wws
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Value("${spring.profiles.active}")
    private String profiles;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {

        registry.addHandler(appWebSocketHandler(), "/app/connect")
                .addInterceptors(new AppWebSocketHandshakeInterceptor())
                .setAllowedOrigins("*");

        registry.addHandler(browserWebSocketHandler(), "/browser/connect")
                .addInterceptors(new BrowserWebSocketHandshakeInterceptor(profiles))
                .setAllowedOrigins("*");

        registry.addHandler(chromePluginWebSocketHandler(), "/browser4pg/connect")
                .addInterceptors(new PluginWebSocketHandshakeInterceptor())
                .setAllowedOrigins("*");
        //电信SWX
        registry.addHandler(swxWebSocketHandler(), "/swx/connect")
                .addInterceptors(new SwxWebSocketHandshakeInterceptor())
                .setAllowedOrigins("*");

    }

    @Bean
    public AppWebSocketHandler appWebSocketHandler(){
        return new AppWebSocketHandler();
    }
    @Bean
    public BrowserWebSocketHandler browserWebSocketHandler(){
        return new BrowserWebSocketHandler();
    }
    @Bean
    public SwxWebSocketHandler swxWebSocketHandler(){
        return new SwxWebSocketHandler();
    }
    @Bean
    public ChromePluginWebSocketHandler chromePluginWebSocketHandler(){
        return new ChromePluginWebSocketHandler();
    }

    @Bean
    public ServletServerContainerFactoryBean createWebSocketContainer() {
        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
        container.setMaxTextMessageBufferSize(8192);
        container.setMaxBinaryMessageBufferSize(8192);
        container.setAsyncSendTimeout(5000L);
        container.setMaxSessionIdleTimeout(1800000L);
        return container;
    }
}
